﻿using LoveEatRabbit.API.Read.Applications.Commands.Permissions;
using LoveEatRabbit.API.Read.DTOS;
using LoveEatRabbit.Domain.RBAC;
using LoveEatRabbit.ErrorCode;
using LoveEatRabbit.Infrastructure.Interfaces;
using MediatR;

namespace LoveEatRabbit.API.Read.Applications.CommandHandlers.Permission
{
    public class PerQueryCommandHandler : IRequestHandler<PerQueryCommand, APIResult<List<PermissionDTO>>>
    {
        private readonly IBaseRespository<PermissionModel> perRep;

        public PerQueryCommandHandler(IBaseRespository<PermissionModel> perRep)
        {
            this.perRep = perRep;
        }

        public Task<APIResult<List<PermissionDTO>>> Handle(PerQueryCommand request, CancellationToken cancellationToken)
        {
            APIResult<List<PermissionDTO>> result = new APIResult<List<PermissionDTO>>();
            result.Code = APIResultCode.查询成功;
            result.Message = APIResultCode.查询成功.ToString();

            var res = from a in perRep.GetAll()
                      where !a.IsDeleted
                      select new PermissionDTO
                      {
                          Index = a.Index,
                          PermissionId = a.PermissionId,
                          PermissionName = a.PermissionName,
                          PermissionURL = a.PermissionURL,
                          
                      };
            if (!string.IsNullOrEmpty(request.PermissionName))
            {
                res=res.Where(x=>x.PermissionName.Contains( request.PermissionName));
            }
            result.Data=res.ToList();
            return Task.FromResult(result);
        }
    }
}
